// Stick everything in the "GravyEditor" namespace
var GravyEditor = new function () {

this.timeout = 100;

this.Previewer = function (input, output) {
    this.input = input;
    this.output = output;
    this.timer = null;
    this.lastText = '';

    // Set up the parser and tokenizer
    this.parser = new Gravy.Parser(new Gravy.HTMLDomGenerator(this.output));
    this.tokenizer = new Gravy.Tokenizer();

    var previewer = this;
    this.input.onkeyup = function () {
        if (previewer.timer === null) {
            previewer.timer = setTimeout(function () {
                previewer.refresh();
            }, GravyEditor.timeout);
        }
    };
}

this.Previewer.prototype.refresh = function () {
    if (this.input.value == this.lastText) {
        this.timer = null;
        return;
    } else {
        this.lastText = this.input.value;
    }

    this.parser.parse(this.tokenizer.tokenize(this.input.value));

    var previewer = this;
    this.timer = setTimeout(function () {
        previewer.refresh();
    }, GravyEditor.timeout);
}

}; // End Namespace

